package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.commons.exception.LoginPhoneException;
import com.woniuxy.commons.utile.BeanCopyUtil;
import com.woniuxy.commons.utile.Result;
import com.woniuxy.controller.dto.GetHousingMaterialDto;
import com.woniuxy.dao.model.HousingMaterial;
import com.woniuxy.service.HousingMaterialService;
import io.lettuce.core.cluster.PubSubClusterEndpoint;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 维修材料字典表 前端控制器
 * </p>
 *
 * @author Keep it simple
 * @since 2022-06-28
 */
@RestController
@Api(tags = "材料上传接口")
@RequestMapping("/housingMaterial")
public class HousingMaterialController {

    @Resource
    private HousingMaterialService housingMaterialService;

    @GetMapping("/getHousingMaterial")
    @ApiOperation("获取全部材料信息")
    public Result getHousingMaterial(){
        List<HousingMaterial> list = housingMaterialService.list(new QueryWrapper<HousingMaterial>().eq("state", 1));
        List<GetHousingMaterialDto> getHousingMaterialDtos = BeanCopyUtil.copyList(list, GetHousingMaterialDto.class);
        return Result.instance().setMessage("查询成功").setCode(200).setData(getHousingMaterialDtos);
    }

    @GetMapping("/getAllMaterialName")
    @ApiOperation("获取全部材料名字种类")
    public Result getAllMaterialName(){
        List<String> allMaterialName = housingMaterialService.getAllMaterialName();
        return Result.instance().setData(allMaterialName);
    }

    @GetMapping("/getMaterialByDictAllName")
    @ApiOperation("通过材料名获取材料信息")
    @ApiImplicitParam(name = "dictAllName" , value = "材料名" ,type = "String")
    public Result getMaterialByDictAllName(String dictAllName){
        System.out.println(dictAllName);
        QueryWrapper<HousingMaterial> wrapper = new QueryWrapper<>();
        wrapper.eq("dictAllName", dictAllName);
        wrapper.eq("state", 1);
        List<HousingMaterial> dictAllName1 = housingMaterialService.list(wrapper);
        if (dictAllName==null)throw new RuntimeException("没有该材料");
        List<GetHousingMaterialDto> getHousingMaterialDto = BeanCopyUtil.copyList(dictAllName1, GetHousingMaterialDto.class);
        return Result.instance().setMessage("查询成功").setCode(200).setData(getHousingMaterialDto);
    }

}

